package com.zj.study.file;

import com.github.liaochong.myexcel.core.SaxExcelReader;
import java.io.ByteArrayInputStream;
import java.util.List;

/**
 * @author 赵赳
 * @since 2024/1/3 11:22
 */
public class ExcelInputUtils {

  private static final Integer MAX_SIZE = 20000;

  /**
   * 解析excel 数据
   *
   * @param FileBytes 文件字节数组
   * @param tClass    转换类
   * @param <T>       转换对象泛型
   * @return 转换java对象数组
   */
  public static <T> List<T> readExcel(byte[] FileBytes, Class<T> tClass) {
    var result = SaxExcelReader.of(tClass)
        .sheet(0)
        .rowFilter(row -> row.getRowNum() > 0)
        .read(new ByteArrayInputStream(FileBytes));
    if (result.isEmpty() || result.size() > MAX_SIZE) {
      throw new RuntimeException("导出数据为空或导出数据超过2万条限制");
    }
    return result;
  }

}
